package com.xy.wsl.api.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;


@Slf4j
public class HttpUtil {
    public static JSONObject doPostEvent(String body, String url) {
        HttpClient client = new HttpClient();

        PostMethod method = new PostMethod(url);
        method.setRequestHeader("Content-type", "application/json; charset=UTF-8");
        method.setRequestBody(body);

        String strResponseData = null;
        try {
            int statusCode = client.executeMethod(method);
//            log.info("statusCode:" + statusCode);
            byte[] responseData = method.getResponseBodyAsString().getBytes(method.getResponseCharSet());
            strResponseData = new String(responseData, "utf-8");

//            log.info("返回数据：" + strResponseData);

        } catch (IOException e) {
            log.info("返回数据错误：" + e.getLocalizedMessage());
            throw new RuntimeException(e);
        }

        method.releaseConnection();

        return  JsonUtil.json2Obj(strResponseData, JSONObject.class);
    }

    public static JSONObject DouyinMsgcheck(String body, String url, String token) {
        HttpClient client = new HttpClient();

        PostMethod method = new PostMethod(url);
        method.setRequestHeader("Content-type", "application/json; charset=UTF-8");
        method.setRequestHeader("X-Token", token);
        method.setRequestBody(body);

        String strResponseData = null;
        try {
            int statusCode = client.executeMethod(method);
//            log.info("statusCode:" + statusCode);
            byte[] responseData = method.getResponseBodyAsString().getBytes(method.getResponseCharSet());
            strResponseData = new String(responseData, "utf-8");

//            log.info("返回数据：" + strResponseData);

        } catch (IOException e) {
            log.info("返回数据错误：" + e.getLocalizedMessage());
            throw new RuntimeException(e);
        }

        method.releaseConnection();

        return  JsonUtil.json2Obj(strResponseData, JSONObject.class);
    }
}
